/*
 * @(#)KeyGenerator.java 1.0 2005-3-1
 * Copyright 2005 VandaGroup, Inc. All rights reserved.
 */
package com.vanda.db.pk;
import java.util.HashMap;
import com.vanda.exception.*;
import com.vanda.db.*;
/**
 * 类说明: <br>
 * 通用主键工具类 - 主键生成器
 * 
 * @author 蒋勇 <br>
 * @version Version 1.00 <br>
 */
public class KeyGenerator {
    /**
     * 容器对象，保存多个主键
     */
    private static HashMap kengens = new HashMap(10);
    /**
     * 缓冲池大小
     */
    private static final int POOL_SIZE = 20;
    /**
     * 主键类
     */
    private KeyInfo keyinfo;

    /**
     * 私有构造函数，保证外界无法直接实例化
     */
    private KeyGenerator() {
    }

    /**
     * 私有构造函数，保证外界无法直接实例化
     */
    private KeyGenerator(String keyName, SQLAccessor accessor, int pool)
            throws VandaException {
        if (pool <= 0) {
            keyinfo = new KeyInfo(POOL_SIZE, keyName, accessor);
        } else {
            keyinfo = new KeyInfo(pool, keyName, accessor);
        }
    }

    /**
     * 方法说明: 静态工厂方法，提供自己的实例
     * 
     * @param keyName
     *            主键的名字
     * @param con
     *            连接对象
     * @param pool
     *            缓冲池大小－注意只能在类的初始化时调用一次有效
     * @return
     * @author 蒋勇 2005-3-1
     */
    public static synchronized KeyGenerator getInstance(String keyName,
            SQLAccessor accessor, int pool) throws VandaException {
        KeyGenerator keygen;
        if (kengens.containsKey(keyName)) {
            keygen = (KeyGenerator) kengens.get(keyName);
            keygen.getKeyinfo().setPoolSize(pool);
        } else {
            keygen = new KeyGenerator(keyName, accessor, pool);
            kengens.put(keyName, keygen);
        }
        return keygen;
    }

    /**
     * 方法说明: 静态工厂方法，提供自己的实例
     * 
     * @param keyName
     *            主键的名字
     * @param con
     *            连接对象
     * @param pool
     *            缓冲池大小－注意只能在类的初始化时调用一次有效
     * @return
     * @author 蒋勇 2005-3-1
     */
    public static synchronized KeyGenerator getInstance(String keyName,
            SQLAccessor accessor) throws VandaException {
        return getInstance(keyName, accessor, 1);
    }

    /**
     * 方法说明: 取值方法，提供下一个合适的键值
     * 
     * @param con
     * @return
     * @author 蒋勇 2005-3-1
     */
    public synchronized long getNextKey(SQLAccessor accessor)
            throws VandaException {
        return keyinfo.getNextKey(accessor);
    }
    
    /**
     * 方法说明: 取当前值
     * 
     * @param con
     * @return
     * @author 蒋勇 2005-3-1
     */
    public long getCurrentKey(SQLAccessor accessor)
            throws VandaException {
        return keyinfo.getCurrentKey(accessor);
    }
    
    /**
     * 设置当前值
     * @param accessor
     * @param value
     * @throws VandaException
     */
    public synchronized void setCurrentValue(SQLAccessor accessor,long value) throws VandaException{
    	keyinfo.setCurrentValue(accessor,value);
    }

    /**
     * @return
     */
    public KeyInfo getKeyinfo() {
        return keyinfo;
    }

    /**
     * @param info
     */
    public void setKeyinfo(KeyInfo info) {
        keyinfo = info;
    }
}